package com.zzq.my.jetpacks.jectpackapp.utils;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class ListUtilGroup {
    private static final String TAG = "ListUtilGroup";
    /**
     * 将一组数据固定分组，每组n个元素
     *
     * @param source 要分组的数据源
     * @param n      每组n个元素
     * @param <T>
     * @return
     */
    public static  <T> List<List<T>> fixedGrouping(List<T> source, int n) {

        if (null == source || source.size() == 0 || n <= 0) {
            return null;
        }
        List<List<T>> result = new ArrayList<List<T>>();
        int remainder = source.size() % n;
        Log.d(TAG, "剩余remainder: "+remainder);//剩余
        int size = (source.size() / n);
        Log.d(TAG, "组size: "+size);//剩余
        for (int i = 0; i < size; i++) {
            List<T> subset = null;
            subset = source.subList(i * n, (i + 1) * n);
            result.add(subset);
        }
        if (remainder > 0) {
            List<T> subset = null;
            subset = source.subList(size * n, size * n + remainder);
            result.add(subset);
        }
        return result;
    }
}
